package com.gzxw.mogublog.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzxw.mogublog.commons.entity.User;
import com.gzxw.mogublog.utils.R;
import com.gzxw.mogublog.xo.service.UserService;
import com.gzxw.mogublog.xo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表 RestController
 *
 * @author gzxw
 * @date 2020年1月4日21:29:09
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/getList")
    public R geList(@RequestBody UserVO userVO) {

        IPage<User> pageList = userService.getList(userVO);
        Map<String, Object> map = new HashMap<>();
        List<User> records = pageList.getRecords();
        long total = pageList.getTotal();
        long size = pageList.getSize();
        long current = pageList.getCurrent();
        map.put("records", records);
        map.put("total", total);
        map.put("size", size);
        map.put("current", current);
        return R.success("返回成功", map);
    }

    @PostMapping("/add")
    public R add(@RequestBody UserVO userVO) {

        String message = userService.addUser(userVO);
        return R.success("200", message);
    }

    @PostMapping("/edit")
    public R edit(@RequestBody UserVO userVO) {

        String message = userService.editUser(userVO);

        return R.success("200", message);
    }

    @PostMapping("/delete")
    public R delete(@RequestBody UserVO userVO) {

        String message = userService.deleteUser(userVO);

        return R.success("200", message);
    }

    @PostMapping("/resetUserPassword")
    public R resetUserPassword(@RequestBody UserVO userVO) {
        String message = userService.resetUserPassword(userVO);
        return R.success("200", message);
    }

}
